package com.qq.first_project.controller.common;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qq.first_project.common.R;
import com.qq.first_project.entity.Friend;
import com.qq.first_project.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Qiu
 * @since 2023-01-30
 */
@RestController
@RequestMapping("/common/friend")
public class FriendController {
    @Autowired
    private FriendService friendService;

    /**
     * 查所有
     * @return
     */
    @GetMapping("list")
    R listAll(){
       List<Friend>  list =friendService.list();
       return R.ok().data("data",list).message("获取友情链接列表成功");
    }

    @GetMapping("page-list")
    R list(ModelMap modelMap, @RequestParam("current") int num,@RequestParam("pageSize") int size, String name){
        Page<Friend> page = new Page<>(num,size);
        IPage<Friend> list = friendService.pageList(page,name);
        modelMap.addAttribute("page",list);
        return R.ok().data("data",list).message("获取友情链接列表成功");
    }

    /**
     * 新增
     * @param friend
     * @return
     */
    @PostMapping("save")
    R save(Friend friend){
        boolean result = friendService.save(friend);
        if (result){
            return R.ok().message("新增成功");
        }else {
            return R.error().message("新增失败");
        }
    }

    /**
     * 修改
     * @param friend
     * @return
     */
    @PostMapping("updateById")
    R updateById(Friend friend){
        boolean result = friendService.updateById(friend);
        if (result){
            return R.ok().message("保存成功");
        }else {
            return R.error().message("保存失败");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("delById")
    R deleteById(Long id){
        boolean result = friendService.removeById(id);
        if (result){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }

}
